package com.ebelter.bpm.activitys;


import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.ebelter.bpm.R;
import com.ebelter.bpm.common.Constant;
import com.ebelter.btcomlib.CommonLib;
import com.ebelter.btcomlib.bases.activity.BaseActivity;
import com.ebelter.btcomlib.models.https.IHttpRequestField;
import com.ebelter.btcomlib.models.https.interfaces.HttpResponse;
import com.ebelter.btcomlib.models.https.responses.RegisterResponse;
import com.ebelter.btcomlib.models.https.responses.getEmailValidateCodeResponse;
import com.ebelter.btcomlib.utils.AppUtils;
import com.ebelter.btcomlib.utils.KeyBoardShowListener;
import com.ebelter.btcomlib.models.https.NetUtils;
import com.ebelter.btcomlib.utils.MD5Utils;
import com.ebelter.btcomlib.utils.SpUtil;
import com.ebelter.btcomlib.utils.ToastUtil;
import com.ebelter.btcomlib.utils.VerifyUtils;
import com.ebelter.btcomlib.utils.ViewUtils;
import com.ebelter.btcomlib.utils.log.LogUtils;
import com.ebelter.btcomlib.views.CombinView1;
import com.ebelter.btcomlib.views.CountDownTextView;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

import static com.ebelter.btcomlib.views.CountDownTextView.LAST_SEND_CODE_TIME;


public class BPM_RegisterActivity extends BaseActivity implements View.OnClickListener {

    private static final String TAG="BPM_RegisterActivity";

    int CMD_WHICH= Constant.IBpm.CMD_PHONE;//默认手机号码注册


    ImageView back_iv;
    CombinView1 name_cb1;
    CombinView1 email_cb1;
    CountDownTextView countDownTextView;
    CombinView1 email_code_cb1;
    CombinView1 p1_cb1;
    CombinView1 p2_cb1;
    TextView done_tv;

    private void FV() {
        back_iv=findViewById(R.id.iv_back_register);
        name_cb1=findViewById(R.id.cb1_bpm_register_nickname);
        email_cb1=findViewById(R.id.cb1_bpm_register_email);
        countDownTextView=findViewById(R.id.page_register_countDownTv);
        email_code_cb1=findViewById(R.id.cb1_bpm_register_emailcode);
        p1_cb1=findViewById(R.id.cb1_bpm_register_p1);
        p2_cb1=findViewById(R.id.cb1_bpm_register_p2);
        done_tv=findViewById(R.id.btn_next_register);


        adaptedVersion();
    }

    private void adaptedVersion() {

        if (AppUtils.getLocalStr().equals(AppUtils.zh)){
            //中国版本
            CMD_WHICH= Constant.IBpm.CMD_PHONE;
            ViewUtils.goneView(countDownTextView);
            ViewUtils.goneView(email_code_cb1);
            email_cb1.setHint1("手机号码");
            email_cb1.setTittle1InputType(0);
        }else {
            //国外版本
            CMD_WHICH= Constant.IBpm.CMD_EMAIL;
            ViewUtils.displayView(countDownTextView);
            ViewUtils.displayView(email_code_cb1);
            email_cb1.setHint1(CommonLib.getString(R.string.str_E_mail));//邮箱
            email_cb1.setTittle1InputType(3);
        }


    }

    private void setListeners() {
        back_iv.setOnClickListener(this);
        done_tv.setOnClickListener(this);
        countDownTextView.setOnClickListener(this);
    }

    @Override
    protected int loadLayoutById() {
        return R.layout.activity_bpm_register;
    }

    @Override
    protected void initView() {
        //监听软键盘的状态
        keyBoardShowListener.setKeyboardListener(new KeyBoardShowListener.OnKeyboardVisibilityListener() {
                    @Override
                    public void onVisibilityChanged(boolean visible) {
                        if (visible) {
                            //软键盘已弹出
                            ViewUtils.hideView(done_tv);
                        } else {
                            //软键盘未弹出
                            ViewUtils.displayView(done_tv);
                        }
                    }
                }, BPM_RegisterActivity.this);

        FV();
        setListeners();
        SpUtil.writeLong(LAST_SEND_CODE_TIME,System.currentTimeMillis());

    }


    @Override
    public void onClick(View v) {
        int i = v.getId();
        if (i==R.id.iv_back_register){
            finish();

        }else if (i == R.id.page_register_countDownTv) {//点击了发送邮件
            getEmailCodeRequest();
        } else if (i == R.id.btn_next_register) {
            if (CMD_WHICH==Constant.IBpm.CMD_PHONE){
                phoneRegister();
            }else if (CMD_WHICH==Constant.IBpm.CMD_EMAIL){
                emailRegister();
            }
            
        }
    }

    private void phoneRegister() {
        String nickName=name_cb1.getTitle1();
        String email=email_cb1.getTitle1();
        String p1=p1_cb1.getTitle1();
        String p2=p2_cb1.getTitle1();

        if (TextUtils.isEmpty(nickName)){
            ToastUtil.show(R.string.Nikename_cannot_be_empty);
            return;
        }
        if (nickName.length()>20){
            ToastUtil.show(R.string.nikename_over_tip);
            return;
        }

        if (TextUtils.isEmpty(email)){
            ToastUtil.show("手机号码不能为空");
            return;
        }
        if (!VerifyUtils.isMobile(email)){
            ToastUtil.show("请输入正确的手机号码");
            return;
        }

        if (TextUtils.isEmpty(p1)){
            ToastUtil.show(R.string.The_Password_cannot_be_empty);
            return;
        }

        if (p1.length()<6){
            ToastUtil.show(R.string.psw_less_tip);
            return;
        }
        if (p1.length()>20){
            ToastUtil.show(R.string.psw_over_tip);
            return;
        }

        if (!TextUtils.equals(p1,p2)){
            ToastUtil.show(R.string.The_password_is_inconsistency_twice);//前后密码输入不一致
            return;
        }
        if (!NetUtils.available()){
            ToastUtil.show(R.string.Network_error);
            return;
        }

        Map<String, Serializable> paramMap = new HashMap<>();

        paramMap.put(IHttpRequestField.nickName,nickName);
        paramMap.put(IHttpRequestField.mobilePhone,email);
        paramMap.put(IHttpRequestField.password, MD5Utils.getMd5(p1));
        paramMap.put(IHttpRequestField.type, 1);

        NetUtils.getInstance().registerUser3(this, paramMap, new HttpResponse<RegisterResponse>() {
            @Override
            public void result(boolean isRequestSuccess, String requestDes, RegisterResponse registerResponse,String s) {
                if (!isRequestSuccess){
                    ToastUtil.show(R.string.Network_connection_error);
                    return;
                }

                if (registerResponse.resultCode==0){
                    LogUtils.i(TAG,"注册返回："+registerResponse.resultMessage);
                    ToastUtil.show(R.string.Registered_successfully);
                    //showRegisterDialog();
                    startActivity(BPM_LoginActivity.class);
                    finish();
                }else if (registerResponse.resultCode==106){
                    ToastUtil.show(R.string.Email_verification_code_has_expired);
                }else if (registerResponse.resultCode==107){
                    ToastUtil.show(R.string.Wrong_email_verification_code);
                }else if (registerResponse.resultCode==103||registerResponse.resultCode==-1){
                    ToastUtil.show(R.string.The_email_or_nickname_has_been_registered);
                } else if (registerResponse.resultCode==110){
                    ToastUtil.show(R.string.An_exception_occurred_on_the_server);
                } else {
                    ToastUtil.show(R.string.Registration_failed);
                }






            }
        });
    }

    //完c
    private void emailRegister() {

        String nickName=name_cb1.getTitle1();
        String email=email_cb1.getTitle1();
        String emailCode=email_code_cb1.getTitle1();
        String p1=p1_cb1.getTitle1();
        String p2=p2_cb1.getTitle1();

        if (TextUtils.isEmpty(nickName)){
            ToastUtil.show(R.string.Nikename_cannot_be_empty);
            return;
        }
        if (nickName.length()>20){
            ToastUtil.show(R.string.nikename_over_tip);
            return;
        }

        if (TextUtils.isEmpty(email)){
            ToastUtil.show(R.string.Email_must_not_be_empty);
            return;
        }
        if (!VerifyUtils.emailFormat(email)){
            ToastUtil.show(R.string.Incorrect_email_format);
            return;
        }

        if (TextUtils.isEmpty(emailCode)){
            ToastUtil.show(R.string.Mail_verification_code_cannot_be_empty);
            return;
        }

        if (TextUtils.isEmpty(p1)){
            ToastUtil.show(R.string.The_Password_cannot_be_empty);
            return;
        }

        if (p1.length()<6){
            ToastUtil.show(R.string.psw_less_tip);
            return;
        }
        if (p1.length()>20){
            ToastUtil.show(R.string.psw_over_tip);
            return;
        }

        if (!TextUtils.equals(p1,p2)){
            ToastUtil.show(R.string.The_password_is_inconsistency_twice);//前后密码输入不一致
            return;
        }
        if (!NetUtils.available()){
            ToastUtil.show(R.string.Network_error);
            return;
        }

        Map<String, Serializable> paramMap = new HashMap<>();
        paramMap.put(IHttpRequestField.nickName,nickName);
        paramMap.put(IHttpRequestField.email,email);
        paramMap.put(IHttpRequestField.code,emailCode);
        paramMap.put(IHttpRequestField.password, MD5Utils.getMd5(p1));
        paramMap.put(IHttpRequestField.type, 1);

        NetUtils.getInstance().registerUser(this, paramMap, new HttpResponse<RegisterResponse>() {
            @Override
            public void result(boolean isRequestSuccess, String requestDes, RegisterResponse registerResponse,String s) {
                if (!isRequestSuccess){
                    ToastUtil.show(R.string.Network_connection_error);
                    return;
                }

                if (registerResponse.resultCode==0){
                    LogUtils.e(TAG,"注册返回："+registerResponse.resultMessage);
                    ToastUtil.show(R.string.Registered_successfully);
                    //showRegisterDialog();
                    startActivity(BPM_LoginActivity.class);
                    finish();
                }else if (registerResponse.resultCode==106){
                    ToastUtil.show(R.string.Email_verification_code_has_expired);
                }else if (registerResponse.resultCode==107){
                    ToastUtil.show(R.string.Wrong_email_verification_code);
                }else if (registerResponse.resultCode==103){
                    ToastUtil.show(R.string.The_email_or_nickname_has_been_registered);
                } else if (registerResponse.resultCode==110){
                    ToastUtil.show(R.string.An_exception_occurred_on_the_server);
                } else {
                    ToastUtil.show(R.string.Registration_failed);
                }

            }
        });



    }


    public void getEmailCodeRequest() {
        String email = email_cb1.getTitle1();
        if (TextUtils.isEmpty(email)){
            ToastUtil.show(R.string.Email_must_not_be_empty);
            return;
        }

        if (!VerifyUtils.emailFormat(email)){
            ToastUtil.show(R.string.Incorrect_email_format);
            return;
        }
        if (!NetUtils.available()){
            ToastUtil.show(R.string.Network_error);
            return;
        }

        NetUtils.getInstance().getEmailValidateCodeRequest(this, email, 1, new HttpResponse<getEmailValidateCodeResponse>() {
            @Override
            public void result(boolean isRequestSuccess, String requestDes, getEmailValidateCodeResponse getEmailValidateCodeResponse,String s) {
                if (!isRequestSuccess){
                    ToastUtil.show(R.string.Network_connection_error);
                }else {
                    LogUtils.i(TAG,requestDes+"----"+getEmailValidateCodeResponse.toString());
                    if (getEmailValidateCodeResponse.resultCode==0){
                        if (countDownTextView!=null) countDownTextView.click();//说明请求成功，按钮进入倒计时模式
                        ToastUtil.show(R.string.The_email_has_been_sent);
                    }else if (getEmailValidateCodeResponse.resultCode==102){//邮箱已经注册
                        ToastUtil.show(R.string.Email_has_been_registered);
                    }else if (getEmailValidateCodeResponse.resultCode==110){//服务器异常
                        ToastUtil.show(R.string.An_exception_occurred_on_the_server);
                    }else {
                        ToastUtil.show(R.string.Failed_to_send_mail);
                    }
                }

            }
        });


    }


//    public void back_iv(View view){
//        LogUtils.i(TAG,"back_iv");
//        finish();
//    }
}
